﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections.Generic;
using Frame.Lib.Core.Log.Operate;
using Frame.Lib.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Common;


public partial class Admin_Log_OperateList :PageList
{

    private LogOperateBO Log
    {
        get
        {
            return LogOperateBO.Instance<LogOperateBO>();
        }
    }

    private string StartTime
    {
        get
        {
            return Request.QueryString["StartTime"];
        }

    }

    private string EndTime
    {
        get
        {
            return Request.QueryString["EndTime"];
        }
    }

    private string PageSize
    {
        get
        {
            return Request.QueryString["PageSize"];
        }
    }

    private string SortDirection
    {
        get
        {
            return (string)ViewState["SortDirection"];
        }
        set
        {
            ViewState["SortDirection"] = value;
        }
    }

    private string SortExpression
    {
        get
        {
            return (string)ViewState["SortExpression"];
        }
        set
        {
            ViewState["SortExpression"] = value;
        }
    }

    private string OrderBy
    {
        get
        {
            return Request.QueryString["OrderBy"];
        }
    }


    //private PagerSortSetting _setting;
    //private CustSort _sorter;
    protected void Page_Load(object sender, EventArgs e)
    {
        //_setting = this.Footer1.InitPager(new Jzy.Core.Common.Web.BindViewHandler(BindView));
        //_sorter = new CustSort(_setting, new BindViewHandler(BindView));
        //_sorter.Sort(btnUserName, btnLoginName, btnUserID, btnInputTime);
        if (!IsPostBack)
        {
            Pager.BindDrp(drpPager);

            if (!string.IsNullOrEmpty(this.PageSize))
            {
                drpPager.SelectedValue = this.PageSize;
            }            
            if (!string.IsNullOrEmpty(this.StartTime))
            {
                txtStartTime.Text = this.StartTime;
            }
            if (!string.IsNullOrEmpty(this.EndTime))
            {
                txtEndTime.Text = this.EndTime;
            }           

            this.SortDirection = "desc";
            this.SortExpression = "InputTime";

            if (!string.IsNullOrEmpty(this.OrderBy))
            {
                this.SortExpression = this.OrderBy.Split(' ')[0];
                this.SortDirection = this.OrderBy.Split(' ')[1];
            }

            //_setting.SortExpression = "InputTime";
            //_setting.SortDirection = Jzy.Core.Common.SortDirection.desc;
            BindView();


            
            
        }
        InitSelectID(hidSelectIDs);
        JSUtil.SetTableHeight("list", 145);
        AddAttr();

        this.IncludeJsFile("calendar", JsFile.Calendar);
    }

    private void AddAttr()
    {
        JSUtil.DelConfirm(btnDelete, "操作日志");
        JSUtil.ClickBtn(lnkDelete, btnDelete);

        WebUtil.SetTimeBox(txtStartTime, false);
        WebUtil.SetTimeBox(txtEndTime, false);

        btnDelete2.Attributes.Add("onclick", "return confirm('确认删除操作日志?')");
        btnClear.Attributes.Add("onclick", "return confirm('确认删除全部操作日志?')");


        JSUtil.ClickBtn(lnkClear, btnClear);
    }

    

    private void BindView()
    {
        LVUtil.Init(lv, lvHeader);
        chkSelectAll.Checked = false;

        switch(this.SortExpression)
        {
            case "InputTime":
                HtmlPager.SetSortImage(btnInputTime, this.SortDirection);
                break;
            case "UserName":
                 HtmlPager.SetSortImage(btnUserName, this.SortDirection);
                break;
            case "LoginName":
                HtmlPager.SetSortImage(btnLoginName, this.SortDirection);
                break;
            case "UserID":
                HtmlPager.SetSortImage(btnUserID, this.SortDirection);
                break;

        }

        PagerSetting setting = new PagerSetting();
        setting.PageIndex = this.PageIndex;
        setting.PageSize = int.Parse(drpPager.SelectedValue);

        if (!string.IsNullOrEmpty(this.SortExpression))
        {
            setting.OrderBy = this.SortExpression + " " + this.SortDirection;
        }
       

        if (txtStartTime.Text != "")
        {
            if (string.IsNullOrEmpty(setting.Where))
            {
                setting.Where += "InputTime >= @p0";
            }
            else
            {
                setting.Where += " and InputTime >= @p0";
            }
        }
        if (txtEndTime.Text != "")
        {
            if (string.IsNullOrEmpty(setting.Where))
            {
                setting.Where += "InputTime <= @p1";
            }
            else
            {
                setting.Where += " and InputTime <= @p1";
            }
        }



        IEnumerable<LogOperateInfo> list = this.Log.GetList(setting, txtStartTime.Text.Trim(), txtEndTime.Text.Trim());

        //Pager.SetPageSize(_setting, drpPager);
        //IEnumerable<LogOperateInfo> list = Log.GetList(_setting, txtStartTime.Text, txtEndTime.Text);
        lv.DataKeyNames = new string[] { "ID" };
        lv.DataSource = list;
        lv.DataBind();

        string prefix = GlobalVar.WebCurrPath + "OperateList.aspx?PageSize=" + drpPager.SelectedValue + "&StartTime=" + txtStartTime.Text + "&EndTime=" + txtEndTime.Text + "&OrderBy=" + setting.OrderBy;



        litPager.Text = HtmlPager.Render(setting, prefix, true);

        //_sorter.SetInitSort();//初始化排序字段
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (strSelectIDs != "")
        {
            Log.Delete(StrUtil.SplitToStr(strSelectIDs));
            BindView();
        }
    }
    protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindView();
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        Log.Delete();
        BindView();
    }

    protected void sort_Click(object sender, EventArgs e)
    {
        if (this.SortDirection == "desc")
        {
            this.SortDirection = "asc";
        }
        else
        {
            this.SortDirection = "desc";
        }

        // HtmlPager.SetSortImage(sender, this.SortDirection);
        IButtonControl button = sender as IButtonControl;
        this.SortExpression = button.CommandArgument;
        BindView();
    }



    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindView();
    }

    protected void btnDelete2_Click(object sender, EventArgs e)
    {
        Log.Delete(txtStartTime.Text, txtEndTime.Text);
        BindView();
    }
}
